package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_tg.domain.TdczbhslBaDetail;
import com.ruoyi.report_tg.domain.TdczbjlbeBaDetail;
import com.ruoyi.report_tg.domain.vo.TdczbhslBaDetailVo;
import com.ruoyi.report_tg.mapper.TdczbhslBaDetailMapper;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdczbhslBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdczbhslBsInfoVo;
import com.ruoyi.report_tg.domain.TdczbhslBsInfo;
import com.ruoyi.report_tg.mapper.TdczbhslBsInfoMapper;
import com.ruoyi.report_tg.service.ITdczbhslBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的承载比（CBR）含水率试验记录主Service业务层处理
 *
 * @author zyb
 * @date 2024-10-29
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class TdczbhslBsInfoServiceImpl implements ITdczbhslBsInfoService {

    private final TdczbhslBsInfoMapper baseMapper;
    private final TdczbhslBaDetailMapper detailMapper;

    /**
     * 查询土的承载比（CBR）含水率试验记录主
     */
    @Override
    @FIllBasicInfo
    public TdczbhslBsInfoVo queryById(Long excelId){
        TdczbhslBsInfoVo tdczbhslBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TdczbhslBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TdczbhslBaDetail>()
            .eq(TdczbhslBaDetail::getExcelId, excelId)
            .orderByAsc(TdczbhslBaDetail::getBaRowNumber));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            tdczbhslBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return tdczbhslBsInfoVo;
    }

    /**
     * 查询土的承载比（CBR）含水率试验记录主列表
     */
    @Override
    public TableDataInfo<TdczbhslBsInfoVo> queryPageList(TdczbhslBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdczbhslBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdczbhslBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的承载比（CBR）含水率试验记录主列表
     */
    @Override
    public List<TdczbhslBsInfoVo> queryList(TdczbhslBsInfoBo bo) {
        LambdaQueryWrapper<TdczbhslBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdczbhslBsInfo> buildQueryWrapper(TdczbhslBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdczbhslBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, TdczbhslBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, TdczbhslBsInfo::getPage, bo.getPage());
//        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdczbhslBsInfo::getJilbh, bo.getJilbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), TdczbhslBsInfo::getJiancdwmc, bo.getJiancdwmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), TdczbhslBsInfo::getGongcbwyt, bo.getGongcbwyt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdczbhslBsInfo::getYangpmc, bo.getYangpmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdczbhslBsInfo::getYangpbh, bo.getYangpbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), TdczbhslBsInfo::getYangpsl, bo.getYangpsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), TdczbhslBsInfo::getYangpzt, bo.getYangpzt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), TdczbhslBsInfo::getYangpsj, bo.getYangpsj());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), TdczbhslBsInfo::getShiyjcrq, bo.getShiyjcrq());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), TdczbhslBsInfo::getShiytjwd, bo.getShiytjwd());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), TdczbhslBsInfo::getShiytjsd, bo.getShiytjsd());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), TdczbhslBsInfo::getJiancyj, bo.getJiancyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), TdczbhslBsInfo::getPandyj, bo.getPandyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), TdczbhslBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), TdczbhslBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), TdczbhslBsInfo::getShebIds, bo.getShebIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), TdczbhslBsInfo::getJiancyjIds, bo.getJiancyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), TdczbhslBsInfo::getPandyjIds, bo.getPandyjIds());
//        lqw.eq(bo.getZuidgmd() != null, TdczbhslBsInfo::getZuidgmd, bo.getZuidgmd());
//        lqw.eq(bo.getZuijhsl() != null, TdczbhslBsInfo::getZuijhsl, bo.getZuijhsl());
        return lqw;
    }

    /**
     * 新增土的承载比（CBR）含水率试验记录主
     */
    @Override
    public Boolean insertByBo(TdczbhslBsInfoBo bo) {
        TdczbhslBsInfo add = BeanUtil.toBean(bo, TdczbhslBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细
            ArrayList<TdczbhslBaDetail> details = new ArrayList<>(18);
            for (long i = 1; i <= 18L; i++) {
                details.add(new TdczbhslBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details, 18);
        }
        return flag;
    }

    /**
     * 修改土的承载比（CBR）含水率试验记录主
     */
    @Override
    @DealHeh
    public Boolean updateByBo(TdczbhslBsInfoBo bo) {
        TdczbhslBsInfo update = BeanUtil.toBean(bo, TdczbhslBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), TdczbhslBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdczbhslBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的承载比（CBR）含水率试验记录主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
